<?php
/**
 * Created by PhpStorm.
 * User: songjian
 * Date: 2017/8/18
 * Time: 下午3:48
 */


namespace Someline\Services;


use GuzzleHttp\Client;


class aplusService
{

    /**
     * @var Client
     */
    protected $client;

    /**
     * @var string
     */
    protected $baseUri = '
http://a.miyingwenhua.com';  //'http://wxb.lanshizi.com/';

    /**
     * @var string
     */
//    protected $clientId = '4';
//    protected $clientKey= "ijQGzfR9" ;// "CKxogkUV";
//    protected $token;
//    protected $access_token;
//    protected $refresh_token;

    /**
     * @var string
     */
    protected $clientSecretKey = '
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjY4ZjBiYTBiNGU5MjVmMGYxMGE2OTRmMjc5NzRhZDM1MGYzZjU5YjcxNGI0NjFlYTM4MjQxZTQ2NmUyYTcwZWM5MjlkZmE4NjI1ODFjY2Y3In0.eyJhdWQiOiIzIiwianRpIjoiNjhmMGJhMGI0ZTkyNWYwZjEwYTY5NGYyNzk3NGFkMzUwZjNmNTliNzE0YjQ2MWVhMzgyNDFlNDY2ZTJhNzBlYzkyOWRmYTg2MjU4MWNjZjciLCJpYXQiOjE1MDIzNDY1NDQsIm5iZiI6MTUwMjM0NjU0NCwiZXhwIjoxNTMzODgyNTQ0LCJzdWIiOiIzMCIsInNjb3BlcyI6W119.iR33kjZxTLHYyWv8HTY9UpACzmwQAbwaGFqi4KgHkxjS1JItVzsoPYLW5uATXpiuuOdqUbp39dsTevrOUpPfpbkPFFZM3lNk_Gvi7-Rixm4N7WBX2r2OQEEMmXpxUBynBrNV5GKsygfJP-_mE-W1E7if-sJebmcz7t1tmqOIcbuwnmZ5SJET6275ulzYmPPFh0jJ0TnkMrmpRXoWWRjwCUtQR9eXDdBgOhCHhfQUVet_jz47v_eHR7SgDTA2Y0iJJE85F3FhJrxiaxcaH_5zhk0SktKVIqjHiz6L39ORURdyVuj0a-fSYHbLnn7MDky98KeRMHjee9Zoy2DJPWZaUlRmvvlRDixjxhC9eTdWjU7pyvl-BUnzY4NuvW6gqirxt_rivnqGRfS0Pz7TF9AwlU5LMXL6GsrjdYLafI6HgABfb0bOTqMM4BGBWhbjyau2TfN6NLVA3pZhZAsvk9EoxNqLBwWVLGZsnnPTM7tE4aaLoys4OlZ5l0PujxpLb-MmYcDtAcY1-xPLzV3hCARp72WvBG8KUT32EZyyBZsGCR7vM0N7zy42VAtrd9o689OcteZuTYS1e3XtHXkemc4MFRhn33R_3Js1PawkcrFNO3D8sQ79AHmo8uzKIiFxCZqZn2bTBOeUit2BEg8ldIceqkZPpnbPe5Qixrj68hJh_rE';

    /**
     * ZhangYueService constructor.
     */
    public function __construct()
    {
        $this->client = new Client([
            'base_uri' => $this->baseUri,
//            'exceptions' => false,
        ]);

//        $this->token = $this->authenticate();


//        $this->access_token = $this->token[0];
//        $this->refresh_token = $this->token[1];
//
//        $this->token = $this->hasFreshToken( $this->refresh_token);
//
//        $this->access_token = $this->token[0];
//        $this->refresh_token = $this->token[1];

    }

    /**
     * @param $id
     * @return string
     */
//    public function getSign($id)
//    {
//        return strtolower(md5($this->clientId . $this->clientSecretKey . $id));
//    }

    /**
     * *
     * authentication
     */

    public function hasFreshToken($refresh_token){

        $response = $this->client->post('/openapi/token/refresh', [

            'query' => [

                'client_key' => $this->clientKey,
                'refresh_token' => $refresh_token,
                'granttype' => 'refresh_token',

            ],

            // 'auth' => ['Token',  $this->token],
        ]);

        if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {

            $backInfo = json_decode((string)$response->getBody(), true);

            return  array($backInfo['access_token'],$backInfo['refresh_token']);

        } else {

            return false;
        }
    }

    public function authenticate()
    {

        $response = $this->client->post('/openapi/token', [

            'query' => [

                'client_key' => $this->clientKey,
                'client_secret' => $this->clientSecretKey,
                'granttype' => 'client_credentials',

            ],

        ]);

        if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {

            $backInfo = json_decode((string)$response->getBody(), true);

            return  array($backInfo['access_token'],$backInfo['refresh_token']);

        } else {
            return false;
        }
    }



    /**
     * @param $bookId
     * @return mixed
     */


    public function fetchResourcesList($access_token)
    {
        $response = $this->client->get('/openapi/resources', [
            'headers' => [
                'Authorization' => 'Token token='.$access_token,
            ],
        ]);

        if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
            return json_decode((string)$response->getBody(), true);
        } else {
            return false;
        }
    }

    public function fetchResourcesItem($resource_key)
    {
        $response = $this->client->get('/openapi/resources/'.$resource_key, [
            'headers' => [
                'Authorization' => 'Token token='.$this->token,
            ],
            'query' => [
                'uid' => $this->clientId,
                'resource_key' => $this->clientKey,
            ],
        ]);

        if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
            return json_decode((string)$response->getBody(), true);
        } else {
            return false;
        }
    }


}